package com.cnap.multiclouldmgmt.scheduleTask;


import cn.hutool.core.lang.UUID;
import com.cnap.multiclouldmgmt.controller.CnapClusterInfoController;
import com.cnap.multiclouldmgmt.model.RepByPage;
import com.cnap.multiclouldmgmt.model.enums.K8sRoleEnum;
import com.cnap.multiclouldmgmt.model.enums.K8sStatusEnum;
import com.cnap.multiclouldmgmt.model.k8sModels.*;
import com.cnap.multiclouldmgmt.model.microSvc.*;
import com.cnap.multiclouldmgmt.model.network.LinkResInfoModel;
import com.cnap.multiclouldmgmt.repository.cnap.MicroSvcMapper;
import com.cnap.multiclouldmgmt.repository.master.MicroSvcUseInfoMapper;
import com.cnap.multiclouldmgmt.service.*;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import com.cnap.multiclouldmgmt.util.QuantityUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;

/**
 * @author dyq
 * 定期处理，获取各种集群信息，用来给展示用，防止特别慢
 */
@Configuration
@EnableScheduling
public class K8ClusterInfoScheduledTask {

    private final static Logger LOGGER = LoggerFactory.getLogger(K8ClusterInfoScheduledTask.class);

    @Autowired
    private K8sClusterService k8sClusterService;

    @Autowired
    private NetworkInfoService networkInfoService;

    @Autowired
    private MicroSvcUseInfoMapper microSvcUseInfoMapper;


    /**
     * 每5分钟获取一下，所有信息，然后放到缓存
     * 只处理没有筛选条件的情况
     */
    @Scheduled(fixedDelay = 5*60*1000)
    public void k8ClusterInfoScheduled() {

        try{

            k8sClusterService.refeshK8sClusterInfos(null);

            //获取网络的情况,先获取个比较大的
            RepByPage<LinkResInfoModel> linkResInfoModels =
                    networkInfoService.getLinkResInfo(1000,1);
            if ((linkResInfoModels.getResult()!= null)
                    &&(!linkResInfoModels.getResult().isEmpty())){
                microSvcUseInfoMapper.bachtInsertLinkRes(linkResInfoModels.getResult());
            }

        }catch(Exception e){
          LOGGER.error(" K8ClusterInfoScheduled failed",e);
        }
    }
}
